package com.bw.controller.order;

import com.bw.controller.dc.PiProcessController;
import com.bw.entity.dc.PiMaterial;
import com.bw.entity.dc.PiProcess;
import com.bw.entity.order.*;
import com.bw.entity.order.center.OrderAndPrice;
import com.bw.req.MaterAndPro;
import com.bw.req.OrderPri;
import com.bw.req.XsPhoto;
import com.bw.service.dc.IPiMaterialService;
import com.bw.service.dc.IPiProcessService;
import com.bw.service.order.*;
import com.bw.entity.order.PiOrderItem;
import com.bw.entity.order.PiOrderLog;
import com.bw.entity.order.center.OrderItemCenter;
import com.bw.entity.user.TbUser;
import com.bw.service.order.IPiOrderItemService;
import com.bw.service.order.IPiOrderLogService;
import com.bw.service.user.ITbUserService;
import com.bw.utils.IdWorker;
import net.sf.excelutils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.resp.ResponseResult;
import com.bw.utils.ExportExcel;
import com.bw.utils.ReadExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.NewThreadAction;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 * 订单表
 *
 * @author yzz
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/order")
public class PiOrderController {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private IPiOrderService iPiOrderService;
    @Autowired
    private IPiOrderItemService orderItemService;//订单零件

    @Autowired
    private IPiOrderItemMaterialService orderItemMaterialService;//订单零件原料表

    @Autowired
    private IPiOrderItemProcessService orderItemProcessService;//订单零件工序表

    @Autowired
    private IPiOrderLogService orderLogService;//订单操作记录

    @Autowired
    private ITbUserService userService;

    //定义日志工具
    private final Logger logger = LoggerFactory.getLogger(PiOrderController.class);


    /**
     * 客户的订单量查询
     * @return
     */
    @GetMapping("/getcustomer")
    private ResponseResult getcustomer(){
        ResponseResult success = ResponseResult.SUCCESS();
        logger.info("客户的订单量查询开始");
        try {
            List<XsPhoto> list = iPiOrderService.getcustomer();
            Map map=new HashMap();
            List name=new ArrayList();
            List value=new ArrayList();
            for (int i=0;i<list.size();i++){
                name.add(list.get(i).getName());
                value.add(list.get(i).getValue());
            }
            map.put("name",name);
            map.put("value",value);
            success.setResult(map);
            logger.debug("客户的订单量查询成功");
            System.out.println("客户的订单量查询"+list);
        }catch (Exception e){
            logger.error("客户的订单量查询失败");
            success.setSuccess(false);
            success.setMessage("客户的订单量查询失败"+e.toString());
        }
        logger.info("客户的订单量查询结束");
        return success;
    }

    /**
     * 获取所有的工艺员
     * @return
     */
    @GetMapping("/getmake")
    private ResponseResult getmake(){
        ResponseResult success = ResponseResult.SUCCESS();
        logger.info("工艺订单量查询开始");
        try {
            List<XsPhoto> list = iPiOrderService.getmake();
            logger.debug("工艺订单量查询成功");
            success.setResult(list);
        }catch (Exception e){
            logger.error("工艺订单量查询失败");
            success.setSuccess(false);
            success.setMessage("工艺订单量查询失败"+e.toString());
        }
        logger.info("工艺订单量查询结束");
        return success;
    }

    /**
     * 获取销售图片
     * @return
     */
    @GetMapping("/xsPhoto")
    private ResponseResult xsPhoto(){
        ResponseResult success = ResponseResult.SUCCESS();
        logger.info("销售图片查询开始");
        try {
            List<XsPhoto> list = iPiOrderService.getXsPhoto();
            logger.debug("销售图片查询成功");
            success.setResult(list);
        }catch (Exception e){
            logger.error("销售图片查询失败");
            success.setSuccess(false);
            success.setMessage("查询失败"+e.toString());
        }
        logger.info("销售图片查询结束");
        return success;
    }

    /**
     * 订单页面查询列表
     *
     * @param num
     * @param size
     * @param id
     * @param piOrder
     * @return
     */
    @PostMapping("/list/{num}/{size}/{id}")
    public ResponseResult list(@PathVariable("num") int num, @PathVariable("size") int size
            , @PathVariable("id") Long id,@RequestBody PiOrder piOrder) {
        logger.info("订单查询查询开始");
        ResponseResult re = new ResponseResult(false, 400, "查询失败");
        try {
            QueryWrapper qw = new QueryWrapper();
            qw.orderByDesc("updateDate");//根据修改时间倒序排序
            qw.ne("isDelete", 1);//排除已经删除的数据
            if (id != null) {
                if(id!=1){//如果是管理员，不加该条件
                    qw.eq("userid", id);//只展示当前登录用户的订单
                }
            }
            if (piOrder.getState() != null) {//根据订单状态查询
                qw.eq("state", piOrder.getState());
            }
            if (piOrder.getCode() != null) {//根据订单编号查询
                qw.like("code", "%" + piOrder.getCode() + "%");
            }
            if (piOrder.getCustomerName() != null) {//根据客户查询
                qw.like("customerName", "%" + piOrder.getCustomerName() + "%");
            }
            if (piOrder.getMin() != null && piOrder.getMax() != null) {//查询订单创建时间的区间
                qw.between("createDate", piOrder.getMin(), piOrder.getMax());
            }
            PageHelper.startPage(num, size);//开启分页
            List olist = iPiOrderService.list(qw);//查询list
            PageInfo pageInfo = new PageInfo(olist);//分页后数据
            re.setResult(pageInfo);//存储
            logger.info("订单查询查询结束");
            return re;
        } catch (Exception e) {
            logger.error("订单查询查询失败,{}", e.toString());
            return re;
        }
    }


    /**
     * 添加工序制作的原料和工序以及费用
     *
     * @param uname
     * @param uid
     * @param oid
     * @param materAndPro
     * @return
     */
    @PostMapping("/saveMaterAndPro/{uname}/{uid}/{oid}/{oi}")
    public ResponseResult saveMaterAndPro(@PathVariable("uname") String uname, @PathVariable("uid") Long uid, @PathVariable("oid") Long oid, @PathVariable("oi") Long oi, @RequestBody MaterAndPro materAndPro) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("添加订单零件价格开始");
        try {
            //添加前先删除
            QueryWrapper qw = new QueryWrapper();
            if (oi != null) {
                qw.eq("orderItemId", oi);
            }
            orderItemMaterialService.remove(qw);
            orderItemProcessService.remove(qw);
            List<PiOrderItemMaterial> itMaterlist = materAndPro.getItMater();//获取工艺原料集合
            orderItemMaterialService.saveBatch(itMaterlist);
            List<PiOrderItemProcess> itProlist = materAndPro.getItPro();//获取工艺工序集合
            orderItemProcessService.saveBatch(itProlist);
            //添加操作日志
            PiOrderLog log = new PiOrderLog();
            log.setOrderId(oid);
            log.setTbId(uid);
            log.setUserName(uname);
            log.setRemarks("添加订单零件价格");
            log.setCreatetime(new Date());
        } catch (Exception e) {
            logger.error("添加订单零件价格失败,{}", e.toString());
            result.setSuccess(false);
            result.setMessage("添加订单零件价格失败");
        }
        logger.info("添加订单零件价格结束");
        return result;
    }

    /**
     * 回显工序制作的原料和工序以及费用
     *
     * @param id
     * @return
     */
    @PostMapping("/hxMaterAndPro/{id}")
    public ResponseResult saveMaterAndPro(@PathVariable("id") Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("回显订单零件价格开始");
        try {
            MaterAndPro materAndPro = new MaterAndPro();
            QueryWrapper qw = new QueryWrapper();
            if (id != null) {//订单零件id与零件材料表orderItemId比较
                qw.eq("orderItemId", id);
            }
            List<PiOrderItemMaterial> mlist = orderItemMaterialService.list(qw);
            materAndPro.setItMater(mlist);//添加到对象中返回
            QueryWrapper qw2 = new QueryWrapper();
            if (id != null) {//订单零件id与工序材料表orderItemId比较
                qw2.eq("orderItemId", id);
            }
            List<PiOrderItemProcess> plist = orderItemProcessService.list(qw2);
            materAndPro.setItPro(plist);
            result.setResult(materAndPro);
            result.setCode(200);
            result.setSuccess(true);
            result.setMessage("回显订单零件价格成功");
        } catch (Exception e) {
            logger.error("回显订单零件价格失败,{}", e.toString());
            result.setSuccess(false);
            result.setMessage("回显订单零件价格失败");
        }
        logger.info("回显订单零件价格结束");
        return result;
    }

    /**
     * 根据订单零件id回显零件信息
     *
     * @param id
     * @return
     */
    @PostMapping("/getItemByOrderItemId/{id}")
    public ResponseResult getItemByOrderItemId(@PathVariable("id") Long id) {
        logger.info("订单回显查询开始");
        ResponseResult re = new ResponseResult(false, 400, "查询失败");
        try {
            PiOrderItem byId = orderItemService.getById(id);
            re.setSuccess(true);
            re.setMessage("查询成功");
            re.setCode(200);
            re.setResult(byId);//存储
            logger.info("订单回显查询结束");
            return re;
        } catch (Exception e) {
            logger.error("订单回显查询失败,{}", e.toString());
            return re;
        }
    }

    /**
     * @param id:
     * @Description:根据状态进行查询
     * @Author: 李钊毅
     * @Date: 2020/6/25 21:30
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("/getOrderByState/{id}/{cus}")
    public ResponseResult getOrderByState(@PathVariable("id") Long id, @PathVariable("cus") String cus) {
        logger.info("订单查询开始");
        ResponseResult re = new ResponseResult(false, 400, "查询失败");
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("state", id);
            queryWrapper.eq("customerName", cus);
            List list = iPiOrderService.list(queryWrapper);
            re.setSuccess(true);
            re.setMessage("查询成功");
            re.setCode(200);
            re.setResult(list);//存储
            logger.info("订单查询结束");
            return re;
        } catch (Exception e) {
            logger.error("订单查询失败,{}", e.toString());
            return re;
        }
    }

    /**
     * @param id:
     * @Description:更具id回显订单
     * @Author: 李钊毅
     * @Date: 2020/6/24 8:49
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("/getById/{id}")
    public ResponseResult getorderById(@PathVariable("id") Long id) {
        logger.info("订单回显查询开始");
        ResponseResult re = new ResponseResult(false, 400, "查询失败");
        try {
            PiOrder byId = iPiOrderService.getById(id);
            re.setSuccess(true);
            re.setMessage("查询成功");
            re.setCode(200);
            re.setResult(byId);//存储
            logger.info("订单回显查询结束");
            return re;
        } catch (Exception e) {
            logger.error("订单回显查询失败,{}", e.toString());
            return re;
        }
    }

    /**
     * 修改状态
     *王诗琪
     * @param id
     * @return
     */
    @PostMapping("/updateByid/{id}")
    public ResponseResult updateByid(@PathVariable("id") Long id, @RequestBody PiOrder piOrder) {
        logger.info("订单根据id修改开始");
        ResponseResult re = new ResponseResult(false, 400, "修改失败");
        try {
            PiOrder byId = iPiOrderService.getById(id);//找到对应的订单
            //修改对应数据
            byId.setPriceIt(piOrder.getPriceIt());
            byId.setUpdateDate(new Date());//添加修改时间
            //保存
            iPiOrderService.saveOrUpdate(byId);
            PiOrder order = new PiOrder();
            order.setUpdateDate(new Date());
            iPiOrderService.save(order);
            re.setSuccess(true);
            re.setMessage("修改成功");
            re.setCode(200);
            logger.info("订单根据id修改结束");
            return re;
        } catch (Exception e) {
            logger.error("订单根据id修改失败,{}", e.toString());
            return re;
        }
    }

    /**
     * 根据id删除材料(需要逻辑删除)
     *
     * @param id
     * @return
     */
    @PostMapping("/del/{id}")
    public ResponseResult delById(@PathVariable("id") Long id) {
        logger.info("原材料逻辑删除开始");
        ResponseResult re = new ResponseResult(false, 400, "删除失败");
        try {
            //查找对应材料
            PiOrder byId = iPiOrderService.getById(id);
            byId.setIsDelete(1);//修改删除状态为1，已删除
            byId.setUpdateDate(new Date());//添加修改时间
            iPiOrderService.saveOrUpdate(byId);//保存
            re.setSuccess(true);
            re.setMessage("删除成功");
            logger.info("原材料逻辑删除结束");
            return re;
        } catch (Exception e) {
            logger.error("原材料逻辑删除失败,{}", e.toString());
            return re;
        }
    }

    /**
     * 文件导入
     *
     * @param file
     * @return
     */
    @PostMapping("/imporExcel")
    public ResponseResult imporExcel(MultipartFile file) {
        logger.info("订单导入开始");
        ResponseResult re = new ResponseResult(false, 400, "上传失败");
        try {
            //获取后缀
            String hou = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf('.'), file.getOriginalFilename().length());
            InputStream in = file.getInputStream();
            //调用封装工具类，处理数据
            List<Map> readExcel = ReadExcel.readExcel(hou, in);
            String jsonString = JSON.toJSONString(readExcel);//转成string
            List<PiOrder> olist = JSON.parseArray(jsonString, PiOrder.class);//转成对象集合
            //保存
            iPiOrderService.saveBatch(olist);
            re.setSuccess(true);
            re.setMessage("上传成功");
            logger.info("订单导入结束");
            return re;
        } catch (Exception e) {
            logger.error("订单导入失败,{}", e.toString());
            return re;
        }
    }

    /**
     * 文件导出:只导出当前选中的订单，生成报价单
     *
     * @param response
     */
    @GetMapping("/exportExcel/{id}")
    public void exportExcel(@PathVariable("id") Long id, HttpServletResponse response) {
        logger.info("订单导出开始");
        try {
            PiOrder byId = iPiOrderService.getById(id);//找到对应的订单(订单号命名)
            OrderPri orderPri = iPiOrderService.findList(id);
            ExportExcel.exportExcel2(response, orderPri, byId.getCode(), "xls\\Quotation.xls");
            logger.info("订单导出结束");
        } catch (Exception e) {
            logger.error("订单导出失败,{}", e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 工序页面列表工艺员看的，只能展示关键信息。只能显示订单状态为已提交的订单信息
     */
    @PostMapping("makelist/{pagenum}/{pagesize}/{userId}")
    public ResponseResult makelist(@PathVariable("pagenum") int pagenum, @PathVariable("pagesize") int pagesize,
                                   @PathVariable("userId") int userId,@RequestBody PiOrder piOrder) { //参数json字符串
        logger.info("工序页面列表查询");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PageHelper.startPage(pagenum, pagesize);//开启分页查询
            QueryWrapper queryWrapper = new QueryWrapper();
            if (piOrder.getCode() != null) {//订单编号查询
                queryWrapper.like("code", "%" + piOrder.getCode() + "%");
            }
            queryWrapper.gt("state", 0);
            queryWrapper.orderByDesc("updateDate");//根据修改时间倒序排序
            queryWrapper.ne("isDelete", 1);//排除已经删除的数据
            if(userId!=1){
                queryWrapper.eq("technicianId",userId);
            }
            queryWrapper.eq("technicianId",userId);
            queryWrapper.eq("technicianId",1);
            List<PiOrder> list = iPiOrderService.list(queryWrapper);
            PageInfo pageInfo = new PageInfo(list);//pagehelper封装好的分页实体类，将数据放进去
            result.setResult(pageInfo);
            result.setSuccess(true);
        } catch (Exception e) {
            logger.error("工序页面列表查询失败,{}", e.toString());
            result.setSuccess(false);
            result.setMessage("工序页面列表查询失败:" + e.toString());
            System.out.println(e.toString());
        }
        logger.info("工序页面列表查询结束");
        return result;
    }

    /**
     * @Description:生成订单添加的id
     * @Author: 李钊毅
     * @Date: 2020/6/20 10:19
     * @return: java.lang.String
     **/
    @GetMapping("getOrderId")
    public String getOrderId() {
        long nextId = idWorker.nextId();
        String ids = String.valueOf(nextId);
        return ids;
    }

    //修改状态
    @PostMapping("updateStatus/{id}/{ck}/{username}/{userid}")
    public ResponseResult updateStatus(@PathVariable("id") Long id, @PathVariable("ck") int ck, @PathVariable("username") String username, @PathVariable("userid") Long userid) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("修改订单状态");
        try {
            PiOrder byId = iPiOrderService.getById(id);
            byId.setState(ck);
            byId.setUpdateDate(new Date());//添加修改时间
            iPiOrderService.saveOrUpdate(byId);
            PiOrderLog piOrderLog = new PiOrderLog();
            if (ck == 0) {
                piOrderLog.setRemarks("未提交");
            } else if (ck == 1) {
                piOrderLog.setRemarks("订单提交");
            } else if (ck == 2) {
                piOrderLog.setRemarks("订单已工艺");
            } else if (ck == 3) {
                piOrderLog.setRemarks("订单已报价");
            } else if (ck == 4) {
                piOrderLog.setRemarks("订单已支付");
            } else if (ck == 5) {
                piOrderLog.setRemarks("已转合同 ");
            } else if (ck == 6) {
                piOrderLog.setRemarks("订单已关闭");
            }
            piOrderLog.setOrderId(id);
            piOrderLog.setUserName(username);
            piOrderLog.setCreatetime(new Date());
            piOrderLog.setTbId(userid);
            orderLogService.save(piOrderLog);
                result.setSuccess(true);
                result.setMessage("修改成功");
        } catch (Exception e) {
            logger.error("修改订单状态失败,{}", e.toString());
            result.setSuccess(false);
            result.setMessage("修改失败");
        }
        logger.info("修改状态结束");
        return result;
    }

    //分配工艺员
    @PostMapping("orderAddMaker/{id}/{ck}/{username}/{userid}/{makerid}")
    public ResponseResult orderAddMaker(@PathVariable("id") Long id, @PathVariable("ck") int ck, @PathVariable("username") String username, @PathVariable("userid") Long userid,@PathVariable("makerid") Long makerid) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("分配工艺员并提交开始执行");
        try {
            PiOrder byId = iPiOrderService.getById(id);
            byId.setState(ck);//提交
            byId.setUpdateDate(new Date());//添加修改时间
            byId.setTechnicianId(makerid);//设置工艺员id
            iPiOrderService.saveOrUpdate(byId);
            PiOrderLog piOrderLog = new PiOrderLog();
            piOrderLog.setRemarks("订单提交，分配工艺员");
            piOrderLog.setOrderId(id);
            piOrderLog.setUserName(username);
            piOrderLog.setCreatetime(new Date());
            piOrderLog.setTbId(userid);
            orderLogService.save(piOrderLog);
            result.setSuccess(true);
            result.setMessage("分配成功");
        } catch (Exception e) {
            logger.error("分配失败,{}", e.toString());
            result.setSuccess(false);
            result.setMessage("分配失败");
        }
        logger.info("分配工艺员并提交执行结束");
        return result;
    }


    /**
     * @Description:保存订单
     * @Author: 李钊毅
     * @Date: 2020/6/22 16:34
     * @return: com.bw.resp.ResponseResult
     **/
   @PostMapping("saveOrder")
    public ResponseResult saveOrder(@RequestBody OrderItemCenter orderItemCenter) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("添加订单状态");

            PiOrderLog orderLog = new PiOrderLog();
            TbUser users = userService.getById(orderItemCenter.getPiOrder().getUserid());
            orderLog.setUserName(users.getFullName());
            orderLog.setTbId(users.getId());
            try {
                if (orderItemCenter.getPiOrder().getId() == null) {
                    orderItemCenter.getPiOrder().setCreateDate(new Date());
                    orderItemCenter.getPiOrder().setState(0);
                    orderItemCenter.getPiOrder().setIsDelete(0);
                    boolean b = iPiOrderService.saveOrUpdate(orderItemCenter.getPiOrder());
                    QueryWrapper wrapper = new QueryWrapper();
                    wrapper.eq("code", orderItemCenter.getPiOrder().getCode());
                    PiOrder one = iPiOrderService.getOne(wrapper);
                    orderLog.setOrderId(one.getId());
                    orderLog.setRemarks("添加操作");
                    orderLog.setCreatetime(new Date());
                    List<PiOrderItem> orderItem = orderItemCenter.getPiOrderItem();
                    for (int i = 0; i < orderItem.size(); i++) {
                        PiOrderItem piOrderItem = orderItem.get(i);
                        piOrderItem.setOrderId(one.getId());
                        piOrderItem.setSn(i + 1);
                    }
                    orderItemService.saveBatch(orderItem);
                    result.setMessage("添加成功");
                } else {
                    orderItemCenter.getPiOrder().setUpdateDate(new Date());
                    iPiOrderService.saveOrUpdate(orderItemCenter.getPiOrder());
                    QueryWrapper wrappers = new QueryWrapper();
                    wrappers.eq("code", orderItemCenter.getPiOrder().getCode());
                    PiOrder one = iPiOrderService.getOne(wrappers);
                    orderLog.setRemarks("修改操作");
                    orderLog.setCreatetime(new Date());
                    orderLog.setOrderId(one.getId());
                    List<PiOrderItem> orderItem = orderItemCenter.getPiOrderItem();
                    for (int i = 0; i < orderItem.size(); i++) {
                        PiOrderItem piOrderItem = orderItem.get(i);
                        piOrderItem.setOrderId(one.getId());
                        piOrderItem.setSn(i + 1);
                    }
                    orderItemService.saveOrUpdateBatch(orderItem);
                    result.setMessage("修改成功");
                }
                orderLogService.save(orderLog);

            } catch (Exception e) {
                logger.error("添加订单状态失败,{}", e.toString());
                result.setSuccess(false);
                result.setMessage("添加失败");
            }
            logger.info("添加状态结束");
            return result;
        }
    /**
     * sql查询每月的订单数和订单价格
     *
     * @return
     */
    @PostMapping("/getOrderAndPrice")
    public ResponseResult getOrderAndPrice() {
        logger.info("查询订单数和价格的统计开始");
        ResponseResult re = new ResponseResult(false, 400, "查询失败");
        try {
            List<PiOrder> plist = iPiOrderService.getOrderAndPrice();
            List<Integer> omuths = new ArrayList<>();
            List<Integer> odays = new ArrayList<>();
            List<BigDecimal> osums = new ArrayList<>();
            List<Integer> onums = new ArrayList<>();
            OrderAndPrice ops = new OrderAndPrice();
            for (PiOrder p : plist) {
                omuths.add(p.getOmouth());
                odays.add(p.getOday());
                osums.add(p.getOsum());
                onums.add(p.getOnum());
                ops.setOmouth(omuths);
                ops.setOday(odays);
                ops.setOsum(osums);
                ops.setOnum(onums);
            }
            System.out.println(ops);
            re.setResult(ops);
            re.setSuccess(true);
            re.setMessage("查询成功");
            logger.info("查询订单数和价格的统计结束");
            return re;
        } catch (Exception e) {
            logger.error("查询订单数和价格的统计错误,{}", e.toString());
            return re;
        }
    }

    /**
     * @Author: 王诗琪
     * 根据订单id查询当前是否有订单零件工序表
     **/
    @PostMapping("getOrder/{codeId}/{ck}/{username}/{userid}")
    public ResponseResult getOrder(@PathVariable("codeId") Long codeId,@PathVariable("ck") Integer ck,
                                   @PathVariable("username") String username,@PathVariable("userid") Long userid) {
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("根据订单id查询订单零件工序表开始");
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("orderId",codeId);
            List<PiOrderItem> list1 = orderItemService.list(queryWrapper);
            for (PiOrderItem l1:list1) {
                QueryWrapper qw = new QueryWrapper();
                qw.eq("orderItemId",l1.getId());
                List<PiOrderItemProcess> list2 = orderItemProcessService.list(qw);
                if(list2.size()==0){
                    result.setMessage("当前不可提交");
                    logger.error("根据订单id查询订单零件工序表失败，当前订单不可提交");
                }else {
                    PiOrder byId = iPiOrderService.getById(codeId);
                    byId.setState(ck);
                    byId.setUpdateDate(new Date());//添加修改时间
                    iPiOrderService.saveOrUpdate(byId);
                    result.setSuccess(true);
                    result.setMessage("提交成功");
                    PiOrderLog piOrderLog = new PiOrderLog();
                    piOrderLog.setRemarks("订单已工艺");
                    piOrderLog.setOrderId(codeId);
                    piOrderLog.setUserName(username);
                    piOrderLog.setCreatetime(new Date());
                    piOrderLog.setTbId(userid);
                    orderLogService.save(piOrderLog);
                }
            }
        } catch (Exception e) {
            logger.error("根据订单id查询订单零件工序表失败,{}", e.toString());
            result.setSuccess(false);
            result.setMessage("根据订单id查询订单零件工序表失败");
        }
        logger.info("根据订单id查询订单零件工序表结束");
        return result;
    }
}

